package nl.saxion.ami;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;



public class DBConnection {

	private static DBConnection uniqueInstance=null;
	private static Connection con = null ;


	private DBConnection(){
		if(!dbExists()){
			System.err.println("de database bestaat niet....") ;
		}
	}

	public static synchronized DBConnection getInstance() {
		if (uniqueInstance==null) {
			uniqueInstance = new DBConnection();
		}
		return uniqueInstance;
	}

	private Boolean dbExists() {
		Boolean exists = false ;
		con = createConnection();
		if(con != null){
			exists = true;
		}
		return(exists) ;
	}

	private Connection createConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (Exception ex) {
		}
		try {
			String url = "jdbc:mysql://localhost/ar_ami";
//			String url = "jdbc:mysql://145.76.19.101:8080/ar_ami";
			
			String username = "root";
			String password = "ami";

			return DriverManager.getConnection(url, username, password);

		} catch (SQLException ex) {
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		} 
		return con;
	}

	public void close() {
		try {
			con.close();
			uniqueInstance=null;
			con=null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection(){
		return con;
	}





}
